package com.dingyh.shopwebbackground.controller;


import com.dingyh.shopwebbackground.entity.Commodity;
import com.dingyh.shopwebbackground.entity.ShopCar;
import com.dingyh.shopwebbackground.service.CommodityService;
import com.dingyh.shopwebbackground.service.ShopCarService;
import com.dingyh.shopwebbackground.util.CommonResult;
import com.dingyh.shopwebbackground.util.UUIDUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dingyuhan
 * @since 2023-02-27
 */
@RestController
@CrossOrigin
@RequestMapping("/shop-car")
public class ShopCarController {

    @Autowired
    ShopCarService shopCarService;

    @Autowired
    CommodityService commodityService;

    @RequestMapping("/getAllCar")
    public CommonResult<Object> getAllCar(String userid,@RequestParam(defaultValue = "1",required = true )int pageIndex, @RequestParam(defaultValue = "10",required = true )int pageSize){
        PageHelper.offsetPage((pageIndex-1)*pageSize,pageSize);
        List<ShopCar> shopCars = shopCarService.getAllCar(userid);
        PageInfo<ShopCar> pageInfo = new PageInfo<>(shopCars);
        return new CommonResult(200,"",pageInfo);
    }

    /**
     * 删除购物车商品
     * @param id
     * @return
     */
    @RequestMapping("/removeWillBuy")
    public CommonResult<Object> removeWillBuy(String id){
        if(shopCarService.removeWillBuy(id)){
            return new CommonResult(200, "删除成功！", null);
        }else{
            return new CommonResult(500, "删除失败！", null);
        }
    }

    /**
     * 更改购物车商品数量
     * @param shopCar
     * @return
     */
    @RequestMapping("/updQty")
    public CommonResult<Object> updQty(ShopCar shopCar){
        if(shopCarService.updQty(shopCar)){
            return new CommonResult(200, "", null);
        }else{
            return new CommonResult(500, "", null);
        }
    }

    /**
     * 加购
     * @param shopCar
     * @return
     */
    @RequestMapping("/addShopCar")
    public CommonResult<Object> addShopCar(ShopCar shopCar){
        ShopCar goods = shopCarService.ifExistGoods(shopCar.getCommodityId(),shopCar.getUserId());
        Commodity commodity = commodityService.selectGoods(shopCar.getCommodityId());
        if ( goods != null ){
            BigDecimal a = goods.getQty().add(shopCar.getQty());
            if (commodity.getStock().compareTo(a) > -1 ){
                goods.setQty(shopCar.getQty().add(goods.getQty()));
                boolean result = shopCarService.updQty(goods);
                if (result) {
                    return new CommonResult(200, "加入购物车成功！", null);
                } else {
                    return new CommonResult(500, "加入购物车失败！", null);
                }
            }else {
                return new CommonResult(200, "该商品库存不足，加购失败！", "1");
            }
        }else {
            ShopCar shopCarCurr = ShopCar.builder()
                    .id(UUIDUtil.generator())
                    .userId(shopCar.getUserId())
                    .commodityId(shopCar.getCommodityId())
                    .qty(shopCar.getQty())
                    .timeAdd(System.currentTimeMillis())
                    .build();
            boolean result = shopCarService.save(shopCarCurr);
            if (result) {
                return new CommonResult(200, "加入购物车成功！", null);
            } else {
                return new CommonResult(500, "加入购物车失败！", null);
            }
        }
    }
}

